import os
from bs4 import BeautifulSoup

footer = '''
  </body>
  <script>
    $('body').css("width", "700px");
    $('body').css('margin', 'auto');
    $('.contribution').css('margin-top', '30px');
  </script>
</html>
'''

htmls = ["/var/www/html/website/apps/CommitGraph/guo.html",
"/var/www/html/website/apps/CommitGraph/chen.html",
"/var/www/html/website/apps/CommitGraph/hu.html",
"/var/www/html/website/apps/CommitGraph/xiao.html",
"/var/www/html/website/apps/CommitGraph/lu.html",
"/var/www/html/website/apps/CommitGraph/huang.html",
"/var/www/html/website/apps/CommitGraph/zhang.html",
"/var/www/html/website/apps/CommitGraph/zhao.html"]


def getContribution(htmlfile):
    name = htmlfile.split('.')[0]
    name = name.split("/")[-1]
    soup = BeautifulSoup(open(htmlfile, 'r').read(), 'html.parser')
    graph_part = str(soup.select(".contribution")[0])
    graph_part = graph_part.replace("贡献度", name + " 贡献度")
    return graph_part


def writeGraph():
    ofile = open('/var/www/html/website/apps/CommitGraph/index.html', 'w')
    
    header_file = open('/var/www/html/website/apps/CommitGraph/header.html', 'r')
    header = header_file.read()
    header_file.close()
    
    ofile.write(header)
    for htmlfile in htmls:
        div_graph = getContribution(htmlfile)
        ofile.write(div_graph)

    ofile.write(footer)
    ofile.close()

if __name__ == "__main__":
    os.system("sh /var/www/html/website/apps/CommitGraph/curl.sh")
    writeGraph()
        
